home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1015 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.7 KB

  1. Path: news.iag.net!news
  2. From: jatmon@iag.net (John R Buchan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: To malloc (new) or not to malloc?  When is the question.
  5. Date: 10 Jan 1996 22:30:52 GMT
  6. Organization: Internet Access Group, Orlando, Florida
  7. Message-ID: <4d1ems$hu7@news.iag.net>
  8. References: <4ctvk3$ort@maverick.tad.eds.com> <pwolf-0901960912510001@pwolf-mac.qualcomm.com>
  9. NNTP-Posting-Host: pm1-orl8.iag.net
  10. X-Newsreader: WinVN 0.99.7
  11.  
  12. In article <pwolf-0901960912510001@pwolf-mac.qualcomm.com>, pwolf@qualcomm.com 
  13. says...
  14. ~
  15. ~In article <4ctvk3$ort@maverick.tad.eds.com>, fignet05.darrins@eds.com
  16. ~(Darrin Smith) wrote:
  17. ~[snip]
  18. ~~ Why is it that you can do something like the following:
  19. ~~         char *x;
  20. ~~         x="Some really long string with no particular meaning";
  21. ~~ and have no problems, but can't (safely) do something like this:
  22. ~~         struct st1{char one[10];
  23. ~~                    char two[20];
  24. ~~                    char three[10];
  25. ~~                   };
  26. ~~         st1 *sptr;      //or struct st1 *sptr in C instead of C++
  27. ~~         sptr=fread(....);
  28. <snip>
  29. ~~
  30. ~First example is incorrect. You may be thinking of declaring with
  31. initializing:
  32.  
  33. What is wrong with the first example? "Some really long string with no
  34. particular meaning" is a string literal, which will be embedded in the exe
  35. somewhere.  In an assignment expression, it decays to a pointer to its
  36. first element, like any char array.  So x is assigned the address where
  37. this literal is embedded in the exe (simplified version, of course).  The
  38. only problem might arise, if he attempts to write to it, since the literal
  39. can legally be stored as read-only.
  40.  
  41. ~   char* x = "Some really long string with no particular meaning";
  42. ~
  43. ~Same thing works for second example if you initialize with constants:
  44. ~   struct st1{char one[10];
  45. ~              char two[20];
  46. ~              char three[10];
  47. ~              }* sptr= {0};
  48. ~
  49. ~The point is that memory allocatiion during initialization is required if
  50. ~you want the pointer to "automatically" point to memory which can be used.
  51. ~In your first example, the "x=" is NOT valid syntax for a sequence of
  52. ~characters. In your second example, memory for the structure was not
  53. ~allocated by your declaration of the pointer, so using the pointer for
  54. ~"fread()" puts information into unallocated memory.
  55.  
  56. ?? This defines a pointer to struct st1, named sptr, and initializes it to
  57. NULL.  Since a NULL pointer is guaranteed not to point to any valid object,
  58. this isn't going to work either.  You will need to either malloc a struct
  59. or define one and set sptr to it.
  60.  
  61.  
  62. -- 
  63. John R Buchan           -:|:-     Looking for that elusive FAQ?  ftp to:
  64. jatmon@mail.iag.net     -:|:-     rtfm.mit.edu /pub/usenet-by-group/....
  65.  
  66.